﻿function InitSUKsData() {
    var obj = $("#tb_portfoliomain");
    var initTab = function () {
        var fildes = {};
        var selectedValues = {};
        var rowSize = 0;
        var fildSize = 0;

        $("#goods_sale .sale_item").each(function () {
            var fName = $(this).find(".prop_name label").text().replace("：", "").replace("*", "");
            var pid = $(this).attr("pid");

            var selected = $(this).find("input:checked");
            if (selected.size() > 0) {
                //计算tr的数量
                rowSize = (rowSize > 0) ? rowSize * selected.size() : selected.size();
                fildes[pid] = fName;    //字段值
                selectedValues[pid] = [];
                fildSize++;

                selected.each(function () {
                    var f = { 'Vid': $(this).val(), 'name': $(this).parent().text(), 'Pid': pid };
                    selectedValues[pid].push(f);
                });
            }
        });

        var divHtml = "";

        //组装值
        if (rowSize > 0) {
            divHtml = "<table cellspacing='1'>";
            divHtml += "<tr>";

            $.each(fildes, function (fi, n) {
                divHtml += '<td>' + n + "</td>";
            });
            divHtml += '<td colspan="2">价格</td>' +
                       '<td colspan="2">数量</td>' +
                       '<td colspan="2">货号</td>' +
                '</tr>';

            divHtml += '<tr class="tb_grid_inout" id="skuTY">';
            $.each(fildes, function () {
                divHtml += "<td>属性</td>";
            });
            divHtml += '<td>统一</td><td><input class="skuTongYi" rel="sku1" type="text" value="" /></td>' +
                       '<td>统一</td><td><input class="skuTongYi" rel="sku2" type="text" value="" /></td>' +
                       '<td>统一</td><td><input class="skuTongYi" rel="sku3" type="text" value="" /></td>' +
                '</tr>';

            var fildesIndex = {};
            for (var i = 0; i < rowSize; i++) {
                divHtml += '<tr class="tb_grid_inout">';

                var k = 0;
                $.each(fildes, function (ii, n) {
                    if (fildesIndex[k] == undefined) {
                        fildesIndex[k] = 0;
                    }
                    var index = fildesIndex[k];

                    //最后一个每次累加
                    if ((k + 1) == fildSize) {
                        fildesIndex[k]++;
                    }

                    //当前累加到最大，前一个累加
                    if (fildesIndex[k] == $(selectedValues[ii]).size()) {
                        if (k > 0) fildesIndex[k - 1]++;
                        fildesIndex[k] = 0;
                    }

                    //如果索引到最大
                    if (index == $(selectedValues[ii]).size()) {
                        index = 0;
                    }

                    divHtml += '<td><input type="hidden" value="' + selectedValues[ii][index].Pid + '|' + selectedValues[ii][index].Vid +
                        '" name="GoodsSku[' + i + '].Code[' + k + ']">' +
                        selectedValues[ii][index].name + 
                        '</td>';
                    k++;
                });

                divHtml += '<td></td><td><input name="GoodsSku[' + i + '].Price" type="text" class="sku1" value="" /></td>' +
                           '<td></td><td><input name="GoodsSku[' + i + '].Amount" type="text" class="sku2" value="" /></td>' +
                           '<td></td><td><input name="GoodsSku[' + i + '].OuterNum" type="text" class="sku3" value="" /></td>' +
                    '</tr>';
            }

            divHtml += '</table>';
        }
        //统一输入
        obj.html(divHtml).find("input.skuTongYi").change(function () {
            $("#tb_portfoliomain input." + $(this).attr('rel')).val($(this).val());
        });
    };

    $("#goods_sale .sale_item input:checkbox").click(function() {
        initTab();
    });

    initTab();
}